﻿using Inovout.Models;
using Inovout.Repositories;
using Inovout.Security.Models;
using Inovout.Security.Repositories;
using Inovout.Services;
using NHibernate.Criterion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inovout.Security.Services
{
    public class UserCredentialService : RepositoryService<UserCredential>, IUserCredentialService
    {
        protected UserCredentialRepository UserCredentialRepository
        {
            get { return (UserCredentialRepository)base.Repository; }
        }

        public DataObjectSet<UserCredential> FindBySystemUserId(int systemUserId)
        {
            return base.Repository.FindAll(Restrictions.Eq("SystemUserId", systemUserId).IgnoreCase()) as DataObjectSet<UserCredential>;
        }

        public UserCredential FindByUserNameIdentifierCode(string name)
        {
            return UserCredentialRepository.FindByUserNameIdentifierCode(name);
        }

        public UserCredential FindUserCredentialByProviderIdentifier(string provider, string identifier)
        {

            UserCredential userCredential = ComponentRegistry.Resolve<IRepository<UserCredential>>().Find(Restrictions.And(Restrictions.Eq("Provider", provider), Restrictions.Eq("Identifier", identifier)));

            if (userCredential != null)
            {
                return userCredential;
            }
            return null;
        }

    }
}
